package com.tff.gmall.cart.controller;

import java.math.BigDecimal;

/**
 * @author Fafa Tuo
 * @version 1.0
 * @date 2021/7/8 23:56
 */
public class TestBigDecimal {
    public static void main(String[] args) {

        //初始化(尽量使用字符串)
        BigDecimal b1 = new BigDecimal(0.01f);
        BigDecimal b2 = new BigDecimal(0.01d);
        BigDecimal b3 = new BigDecimal("0.01");

        BigDecimal b4 = new BigDecimal("4");
        BigDecimal b5 = new BigDecimal("2");

        System.out.println("b1 = " + b1);
        System.out.println("b2 = " + b2);
        System.out.println("b3 = " + b3);

        //比较
        int i = b1.compareTo(b2);// 1 0 -1
        System.out.println("i = " + i);

        //运算
        System.out.println(b4.add(b5));
        System.out.println("b4.subtract(b5) = " + b4.subtract(b5));
        System.out.println("b4.multiply(b5) = " + b4.multiply(b5));
        BigDecimal divide = b4.divide(b5,2,BigDecimal.ROUND_HALF_DOWN);
        System.out.println("divide = " + divide);

        //约数
        BigDecimal subtract = b2.subtract(b1);
        System.out.println("subtract = " + subtract);
        BigDecimal bigDecimal = subtract.setScale(3, BigDecimal.ROUND_HALF_DOWN);
        System.out.println("bigDecimal = " + bigDecimal);
    }
}
